//
//  CMSServicesHandler+CMSResource.m
//  CMSCharts
//
//  Created by symbio on 2023/2/14.
//

#import "CMSServicesHandler+CMSResource.h"
#import <CMSFoundation/NSObject+CMSProperty.h>

@interface CMSServicesHandler (CMSResource)

@property (nonatomic, strong) NSMutableDictionary *imgCacheDic;
@end

@implementation CMSServicesHandler (CMSResource)

- (NSMutableDictionary *)imgCacheDic {
    if ([self cms_valueForKey:@"imgCacheDic"] == nil) {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [self cms_setValue:dict forKey:@"imgCacheDic"];
        return dict;
    }
    return [self cms_valueForKey:@"imgCacheDic"];
}

- (void)setImgCacheDic:(NSMutableDictionary *)imgCacheDic {
    [self cms_setValue:imgCacheDic forKey:@"imgCacheDic"];
}

- (void)openURI:(NSString *)uri {
    NSLog(@"AppDelegate 中 uri==%@",uri);
}

- (void)getImageForUrl:(NSString * _Nullable)url completeBlock:(void (^ _Nullable)(UIImage * _Nullable))completeBlock {
    if (self.imgCacheDic[url]) {
        completeBlock(self.imgCacheDic[url]);
    }
    else {
        if (completeBlock && [url containsString:@"appres/skinImage/"]) {
            //        url = [url stringByReplacingOccurrencesOfString:@".png" withString:@"@3x.png"];
            NSString *path = [[NSBundle mainBundle] pathForResource:url ofType:nil];
            UIImage *image1 = [UIImage imageWithContentsOfFile:path];
            UIImage *imgOk = [UIImage imageWithCGImage:image1.CGImage scale:3 orientation:UIImageOrientationUp];
            self.imgCacheDic[url] = imgOk;
            completeBlock(imgOk);
        }
        else if (completeBlock && url) {
            if ([url containsString:@"http"]) {
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        UIImage *image1 = [UIImage imageWithData:data scale:3];
                        self.imgCacheDic[url] = image1;
                        completeBlock(image1);
                    });
                });
            }
            else {
                UIImage *image1 = [UIImage imageNamed:url];
                self.imgCacheDic[url] = image1;
                completeBlock(image1);
            }
        }
    }
}

- (void)getJsonConfigForPath:(NSString * _Nullable)path completeBlock:(void (^ _Nullable)(id _Nullable))completeBlock {
    [self getJsonConfigForRemotePath:nil defaultPath:path completeBlock:completeBlock];
}

- (void)getJsonConfigForRemotePath:(NSString * _Nullable)remotePath defaultPath:(NSString * _Nullable)defaultPath completeBlock:(void (^ _Nullable)(id _Nullable))completeBlock {
    if (remotePath.length > 0) {

    }
    else{
        if (completeBlock && [defaultPath containsString:@"appres/quotes_cfg/"]) {
            NSString *pathJson = [[NSBundle mainBundle] pathForResource:defaultPath ofType:nil];
            NSData *data = [[NSData alloc] initWithContentsOfFile:pathJson];
            NSError *error;
            id object;
            @try {
                object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
            } @catch (NSException *exception) {
            } @finally {}
            completeBlock(object);
        }
    }
}

- (void)getResourceForUrl:(NSString * _Nullable)url completeBlock:(void (^ _Nullable)(NSData * _Nullable))completeBlock {
    if (completeBlock && [url containsString:@"CMSIconTitleItem"]) {
        completeBlock(UIImagePNGRepresentation([UIImage imageNamed:@"资讯中心"]));
    }
    else if (completeBlock && [url containsString:@"hot"]) {
        completeBlock(UIImagePNGRepresentation([UIImage imageNamed:@"hot"]));
    }
    else if (completeBlock && [url containsString:@"icon-"]) {
        completeBlock(UIImagePNGRepresentation([UIImage imageNamed:url]));
    }
    else if (completeBlock && [url containsString:@"mine/res/"]) {
        NSString *imgName = [NSString stringWithFormat:@"mine_%@.png",[url componentsSeparatedByString:@"mine/res/"].lastObject];
        completeBlock(UIImagePNGRepresentation([UIImage imageNamed:imgName]));
    }
    else if (completeBlock && [url containsString:@"appres/"]) {
        NSString *path = [[NSBundle mainBundle] pathForResource:url ofType:nil];
        completeBlock([NSData dataWithContentsOfFile:path]);
    }
    else {
        completeBlock(UIImagePNGRepresentation([UIImage imageNamed:url]));
    }
}

- (NSString * _Nullable)currentFontConfigKeyWithAdapter:(BOOL)isAdapter {
    return (isAdapter && [UIScreen mainScreen].bounds.size.width < 375) ? @"smallFont" : @"normalFont";
}

- (BOOL)isAccessory {
    return false;
}

- (NSData * _Nullable)loadCustomFontData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"appres/font/CMS1.0.otf" ofType:nil];
    return [NSData dataWithContentsOfFile:path];
}

- (nonnull NSDictionary *)loadFontConfigWithAccessory:(BOOL)isAccessory {
    NSString *string = @"appres/font/font_size_config.json";
    if (isAccessory) {
        string = @"appres/font/font_size_config_accessory.json";
    }
    __block NSString *path = [[NSBundle mainBundle] pathForResource:string ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error;
    id object;
    @try {
        object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    } @catch (NSException *exception) {}
    return object;
}

@end
